import { AuthenticationService, httpRequestService, TokenService } from 'my-web-core/services';

export default class AuthService {
  protected readonly url = '/authentication';

  async checkOnUsername(username: string): Promise<boolean> {
    const response = await httpRequestService.get(`${this.url}/checkOnUsername`, {
      data: {
        username: username,
      },
    });

    return response.payload.usernameExists;
  }

  /**
   * Login with provided [username] and [password]. The user basic info will be return.
   */
  async login(username: string, password: string): Promise<any> {
    // TODO: Applies real deviceInfo & systemInfo as needed.
    const response = await httpRequestService.post(`${this.url}/login`, {
      data: {
        username: username,
        password: password,
        deviceInfo: {},
        systemInfo: {},
      },
    });

    return response.payload;
  }

  logout(): void {
    // Removes local user info.
    new AuthenticationService().removeUserInfo();

    // Removes local token info.
    TokenService.removeToken();
  }
}
